<?php
class flash implements ArrayAccess
{   
    function __construct()
    {
	if(!array_key_exists("_flash", $_SESSION) || 
	   !is_array($_SESSION["_flash"]))
	  $_SESSION["_flash"] = array();
    }
    
    function offsetExists($offset)
    {
	if(array_key_exists($offset, $_SESSION["_flash"]))
	  return true;
	else
	  return false;	   
    }
    
    function offsetGet($offset)
    {
	if(is_array($_SESSION["_flash"]) && 
	   array_key_exists($offset, $_SESSION["_flash"]))
	  return $_SESSION["_flash"][$offset]["val"];
	return null;
    }
    
    function offsetSet($offset, $value)
    {
	$_SESSION["_flash"][$offset] = array("val" => $value);
    }
    
    function offsetUnset($offset)
    {
	$_SESSION["_flash"][$offset] = null;
	unset($_SESSION["_flash"][$offset]);
    }

    function keep($offset)
    {
	$_SESSION["_flash"][$offset]["keep"] = true;
    }
        
    function cleanup()
    {
	if(!array_key_exists("_flash", $_SESSION))
	  return;
	
	while (list($key, $val) = each($_SESSION["_flash"]))
	{
	    if(!is_array($val))
	      continue;
	    
	    if(array_key_exists("keep", $val) && $val["keep"])
	    {
		$vak["keep"] = false;
		continue;
	    }
	    
	    $_SESSION["_flash"][$key] = null;
	    unset($_SESSION["_flash"][$key]);
	}
    }
}
?>
